// sharing/pages/user/mylist/index.js
var app = getApp();
Page({
	TOAPPID: require('../../../resource/components/footer/footer.js'),
  /**
   * 页面的初始数据
   */
  data: {
    showModalStatus: false,
    pid: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.footerInit(that);
    app.getUserInfo(function (openid) {
      app.util.request({
        url: 'entry/wxapp/myList',
        data: { openid: openid, m: 'junsion_wifinav' },
        cachetime: 0,
        success: function (res) {
          var data = res.data;
          if (data.code == 1) {
            that.setData({ list: data.list, packages: data.packages });
          }
        }
      });
    });
    app.getShareData(function (res) { that.setData({ cfg: res.data }) });
  },
  onShareAppMessage: function () {
    return {
      title: this.data.cfg.indexShare,
      path: '/sharing/pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  onPullDownRefresh: function () {
    this.onLoad();
    wx.stopPullDownRefresh();
  },
  preview: function (e) {
    var list = this.data.list;
    var idx = e.currentTarget.dataset.idx;
    wx.previewImage({
      urls: [list[idx].advPic],
    });
  },
  xufei: function (e) {
    var that = this;
    var list = that.data.list;
    var pid = that.data.pid;
    var idx = that.data.idx;
    if(!pid){
	  wx.showModal({
        title: '温馨提示',
        content: '请选择套餐',
        showCancel: false,
        success: function (r) {
	        if (r.confirm) {
	        	that.util('open');
	        }
	      }
      });
	    return ;
    }
    app.getUserInfo(function (openid) {
      app.util.request({
        url: 'entry/wxapp/renew',
        data: { openid: openid, idx: list[idx].id, pid: pid, m: 'junsion_wifinav' },
        cachetime: 0,
        success: function (res) {
          if (res.data.code == 1) {
            wx.requestPayment({
              timeStamp: res.data.params.timeStamp,
              nonceStr: res.data.params.nonceStr,
              package: res.data.params.package,
              signType: res.data.params.signType,
              paySign: res.data.params.paySign,
              success: function (res) {
                wx.showModal({
                  title: '温馨提示',
                  content: '支付成功',
                  showCancel: false,
                  success: function (r) {
                    if (r.confirm) {
                      that.onLoad();
                    }
                  }
                });
              }
            });
          } else if (res.data.code == 10) {
    	    wx.showModal({
              title: '温馨提示',
              content: '续费成功',
              showCancel: false,
              success: function (r) {
                if (r.confirm) {
                  that.onLoad();
                }
              }
            });
          } else if (res.data.code == 2) {
        	  wx.showModal({
        		  title: '温馨提示',
        		  content: res.data.msg,
        		  showCancel: false,
        		  success: function (r) {
        			  if (r.confirm) {
        				  that.util('open');
        			  }
        		  }
        	  });
          } else {
            wx.showModal({
              title: '温馨提示',
              content: '系统错误',
              showCancel: false,
              success: function (r) {
  		        if (r.confirm) {
  		            that.util('open');
  		          }
  		        }
            });
          }
        }
      });
    });
  },
  powerDrawer: function (e) {
    var currentStatu = e.currentTarget.dataset.statu;
    var idx = e.currentTarget.dataset.idx;
    if (idx >= 0) this.setData({idx: idx});
    this.util(currentStatu)
  },
  onImg: function(e){
    wx.previewImage({
      urls: [e.currentTarget.dataset.src],
    })
  },  
  util: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例 
    var animation = wx.createAnimation({
      duration: 200, //动画时长 
      timingFunction: "linear", //线性 
      delay: 0 //0则不延迟 
    });

    // 第2步：这个动画实例赋给当前的动画实例 
    this.animation = animation;

    // 第3步：执行第一组动画 
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存 
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画 
    setTimeout(function () {
      // 执行第二组动画 
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象 
      this.setData({
        animationData: animation
      })

      //关闭 
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)

    // 显示 
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
    
    // 确定 
    if (currentStatu == "confrim") {
    	this.setData({
			showModalStatus: false
		});
		this.xufei();
    }
  },
  selPackage: function(e){
	  var pid = e.currentTarget.dataset.pid;
	  this.setData({pid: pid});
  },
  goEdit: function(e){
	  var idx = e.currentTarget.dataset.idx;
	  var list = this.data.list;
	  wx.setStorageSync('listId',list[idx].id);
	  wx.switchTab({
		  'url': '../../enter/enter'
	  });
  },
  goDel: function(e){
	  var that = this;
	  wx.showModal({
		  title: '温馨提示',
		  content: '确定删除吗？',
		  success: function(r){
			  if(r.confirm){
				  var list = that.data.list;
				  var idx = e.currentTarget.dataset.idx;
				  var data = {
						  id: list[idx].id,
						  m: 'junsion_wifinav',
						  op: 'del'
				  }
				  app.util.request({
					  url: 'entry/wxapp/updateWifi',
					  data: data,
					  cachetime: 0,
					  success: function (res) {
						  if(res.data.code == 1){
							  wx.showModal({
								  title: '温馨提示',
								  content: '删除成功',
								  showCancel: false,
								  success: function(){
									  list.splice(idx, 1);
									  that.setData({list: list});
								  }
							  });
						  }else{
							  wx.showModal({
								  title: '温馨提示',
								  content: '删除失败',
								  showCancel: false,
							  });
						  }
					  }
				  });
			  }
		  },
	  });
  }
})